package com.six.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class TokenUtils {


    //设置过期时间
    private static final long EXPIRE_DATE=30*60*100000;

    //token秘钥
    private static final String TOKEN_SECRET = "sfsafsffafafaASFSAFAFASsavasvavSAVAVAW&&**&&&SAFASFSsdvsdvaaSscdsvdvsvsvsccvdaav346sv";

    /**
     * 生成token
     * @param name
     * @param phone
     * @return
     */
    public static String token (Long id,String name,String phone,String image,String username,String email,String perms){

        String token = "";
        try {
            //过期时间
            Date date = new Date(System.currentTimeMillis()+EXPIRE_DATE);
            //秘钥及加密算法
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            //设置头部信息
            Map<String,Object> header = new HashMap<>();
            header.put("typ","JWT");
            header.put("alg","HS256");


            //携带username，password信息，生成签名
            token = JWT.create()
                    //设置头部
                    .withHeader(header)
                    //设置载荷
                    .withClaim("id",id)
                    .withClaim("name",name)
                    .withClaim("phone",phone)
                    .withClaim("image",image)
                    .withClaim("username",username)
                    .withClaim("email",email)
                    .withClaim("perms",perms)
                    .withExpiresAt(date)
                    //加密
                    .sign(algorithm);
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }
        return token;
    }

    /**
     * 校验
     * @param token
     * @return
     */
    public static boolean verify(String token){
        /**
         * @desc   验证token，通过返回true
         * @params [token]需要校验的串
         **/
        try {
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            JWTVerifier verifier = JWT.require(algorithm).build();
            DecodedJWT jwt = verifier.verify(token);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return  false;
        }
    }


    public static void main(String[] args) {


//        String token = token("班长", "110", "1100", "1100", "11");
//
//        System.out.println(token);

//        eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpbWFnZSI6IjExMDAiLCJwaG9uZSI6IjExMCIsIm5hbWUiOiLnj63plb8iLCJleHAiOjE2NDI5MTI3OTYsImVtYWlsIjoiMTEiLCJ1c2VybmFtZSI6IjExMDAifQ.iC3PQ6yJ0nP8JY-OJbQD_bEM4l_RwfgEW-ko9sQVfXI

        boolean verify = verify("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpbWFnZSI6IjExMDAiLCJwaG9uZSI6IjExMCIsIm5hbWUiOiLnj63plb8iLCJleHAiOjE2NDI5MTI3OTYsImVtYWlsIjoiMTEiLCJ1c2VybmFtZSI6IjExMDAifQ.iC3PQ6yJ0nP8JY-OJbQD_bEM4l_RwfgEW-ko9sQVfXI");


        System.out.println(verify);
    }


}